﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.Entities;
using MovieRental.DataInterfaces;
using MovieRental.EfRepositoryLibrary.Mappers;
using MovieRental.GeneralDataExceptions;

namespace MovieRental.EfRepositoryLibrary
{
    public class EfTemporaryDiscountRepository 
        : EfRepository<TemporaryDiscountInfo, Guid, TemporaryDiscount, Discount>, ITemporaryDiscountRepository
    {
        public EfTemporaryDiscountRepository(string connectionString)
            : base(connectionString)
        {
            _mapper = new TemporaryDiscountMapper();
        }

        public override void Delete(Guid id)
        {
            using (var context = CreateContext())
            {
                foreach (var edition in context.Editions.Where(x => x.TemporaryDiscountId == id))
                    edition.TemporaryDiscountId = null;
                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new DbConnectionException("Can't connect to the database.", ex);
                }
            }
            base.Delete(id);
        }
    }
}
